package com.zhengsr.wanandroid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhengsr.corelib.bean.ArticleCollectBean;
import com.zhengsr.corelib.bean.BannerBean;
import com.zhengsr.corelib.bean.BaseResponse;
import com.zhengsr.corelib.bean.CollectBean;
import com.zhengsr.wanandroid.Constant;
import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.Utils;
import com.zhengsr.wanandroid.activity.LoginInActivity;
import com.zhengsr.wanandroid.mvp.base.BaseFragment;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.present.HomePresent;
import com.zhengsr.wanandroid.ui.main.BannerView;
import com.zhengsr.wanandroid.ui.main.adapter.HomeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment<HomePresent>
        implements ContractManager.IHomeView, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, BannerView.BannerItemClickListener {
    private static final String TAG = "HomeFragment";

    private HomePresent mHomePresent;

    @BindView(R.id.home_recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.normal_content)
    SmartRefreshLayout mSmartRefreshLayout;
    private BannerView mBannerView;
    private HomeAdapter mHomeAdapter;
    private List<CollectBean> mArticleBeans = new ArrayList<>();



    public static HomeFragment newInstance(){
        return new HomeFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public HomePresent getPresent() {
        mHomePresent = HomePresent.create(this);
        return mHomePresent;
    }

    @Override
    public void reload() {
        super.reload();
        if (mHomePresent != null){
            mHomePresent.reload();
        }
    }

    /**
     * 懒加载的时候采取加载view
     * @param view
     */
    @Override
    public void initView(View view) {
        LinearLayoutManager manager = new LinearLayoutManager(_mActivity);
        mRecyclerView.setLayoutManager(manager);
        mHomeAdapter = new HomeAdapter(R.layout.item_article_recy_layout, mArticleBeans);
        mBannerView = new BannerView(_mActivity);
        mHomeAdapter.addHeaderView(mBannerView);
        mRecyclerView.setAdapter(mHomeAdapter);
        mHomeAdapter.setOnItemChildClickListener(this);
        mHomeAdapter.setOnItemClickListener(this);

        mBannerView.setBannerItemClickListener(this);


        initRefreshlayout();

    }

    private void initRefreshlayout() {
        mSmartRefreshLayout.setEnableLoadMore(true);
        mSmartRefreshLayout.setEnableRefresh(true);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mHomePresent.onRefresh();
                mSmartRefreshLayout.finishRefresh(1000);

            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mHomePresent.loadMore();
                mSmartRefreshLayout.finishLoadMore(1000);
            }
        });



    }

    @Override
    protected void getLocalReceiver(Intent intent) {
        super.getLocalReceiver(intent);
        int id = intent.getIntExtra(Constant.KEY_ARTICLE_ID,-1);
        if (id != -1){
            CollectBean bean = mArticleBeans.get(id);
            if (bean != null) {
                bean.setCollect(intent.getBooleanExtra(Constant.KEY_ARTICLE_COLLECT,false));
                mHomeAdapter.setData(id, bean);
            }
        }
    }

    @Override
    public void loadMoreData(List<CollectBean> articleBeans) {
        mArticleBeans.addAll(articleBeans);
        mHomeAdapter.notifyDataSetChanged();
    }


    @Override
    public void loadDataSuccess(List<BannerBean> bannerBeans, List<CollectBean> articleBeans) {
        mArticleBeans.clear();
        mArticleBeans.addAll(articleBeans);
        mBannerView.setData(bannerBeans);
        mHomeAdapter.notifyDataSetChanged();


    }

    @Override
    public void addCollectArticleSuccess(int position, CollectBean collectBean,
                                         BaseResponse<ArticleCollectBean> articleCollectBean) {
        if (articleCollectBean.getErrorCode() == 0){
            mHomeAdapter.setData(position,collectBean);
            Toast.makeText(_mActivity, "收藏成功", Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void cancelCollectArticleSuccess(int position, CollectBean collectBean, BaseResponse response) {
        if (response.getErrorCode() == 0){
            mHomeAdapter.setData(position,collectBean);
            Toast.makeText(_mActivity, "取消收藏成功", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch(view.getId()){
            case R.id.item_search_pager_like_iv:
                if (!mHomePresent.getLoginStatus()){
                    Toast.makeText(_mActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(_mActivity, LoginInActivity.class));
                    return;
                }
                CollectBean bean = mArticleBeans.get(position);
                if (bean != null){
                    if (!bean.isCollect()) {
                        mHomePresent.addCollectArticle(position, bean);
                    }else{
                        mHomePresent.cancelCollectArticle(position,bean);
                    }
                }
                 break;
            default :
                break;

        }

    }

    @Override
    protected boolean showNormalView() {
        return false;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        CollectBean bean = mArticleBeans.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("url",bean.getLink());
        bundle.putString("title",bean.getTitle());
        bundle.putInt("position",position);
        bundle.putInt("id",bean.getId());
        bundle.putInt("collect_state",bean.isCollect() ? 1:0);
        Utils.startAricleActivity(view,_mActivity,bundle);
    }

    @Override
    public void itemClick(View view, BannerBean bannerBean) {
        Bundle bundle = new Bundle();
        bundle.putString("url",bannerBean.getUrl());
        bundle.putString("title",bannerBean.getTitle());
        bundle.putInt("id",bannerBean.getId());
        bundle.putInt("collect_state",-1);
        Utils.startAricleActivity(view,_mActivity,bundle);
    }


}
